<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="lib/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="lib/admin/admin.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<!--<script type="text/html" id="barDemo">-->
<!--    <a class="layui-btn layui-btn-xs" lay-event="edit">查看</a>-->
<!--    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="submitAudit">提交审核</a>-->
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
<!--</script>-->
<script type="text/html" id="switchStatus">
    {{# if(d.statusName== '新录入' || d.statusName== '审核不通过' ){ }}
    <a class="layui-btn layui-btn-xs" lay-event="views">查看</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="submitAudit">提交审核</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delWhEntryR">删除</a>

    {{# }else if(d.statusName== '待审核'){ }}

    <a class="layui-btn layui-btn-xs" lay-event="views">查看</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="cancelAudit">取消审核</a>

    <!--    <input type="text" value="{{ d.statusName}}">-->
    {{# }else if(d.statusName== '审核通过') { }}
    <a class="layui-btn layui-btn-xs" lay-event="views">查看</a>
    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="onShelf">上架</a>

    {{# }else if(d.statusName== '进行中') { }}
    <a class="layui-btn layui-btn-xs" lay-event="views">查看</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal " lay-event="viewOnShelf">查看进度</a>

    {{# }else { }}
    <a class="layui-btn layui-btn-xs" lay-event="views">查看</a>
    {{# } }}
</script>

<fieldset class="layui-elem-field layui-field-title">
    <legend>入库单管理</legend>
</fieldset>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">

                    <div class="layui-inline">
                        <label class="layui-form-label">入库单</label>
                        <div class="layui-input-inline" style="margin-right: 10px">
                            <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入您想搜索的入库单" id="whEntryCode" class="layui-input" style="width: 250px">
                        </div>
                        <div class="layui-input-inline">
                            <button type="button" class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search" onclick="selectWhER()">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                <div class="layui-card-body">
                    <button type="button" class="layui-btn" onclick="submitAudit()">批量提交审核</button>
                    <table class="layui-hide" id="test" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>


<!--<button type="button" class="layui-btn"  onclick="insertDelivery()">批量收货</button>-->

<script src="lib/jquery-1.12.4.js"></script>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->


<script>

    layui.use(['table','layer'], function(){
        var table = layui.table;
        var layer = layui.layer;
        //监听行工具事件
        table.render({
            elem: '#test'
            ,url:'pk/warehouseEntry/listWhEntryR',
            parseData: function(res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": 1, //解析数据长度
                    "data": res.obj.list //解析数据列表
                };
            }
            ,page:true
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,cols: [[
                {type:'checkbox'}
                ,{field:'whEntryCode', title: '入库单号', sort: true}
                ,{field:'createDate',title: '创建时间', sort: true}
                ,{field:'staffName', title: '创建人名称', sort: true}
                ,{field:'statusName', title: '状态', sort: true}
                ,{field:'typeName', title: '入库单类型', sort: true}
                ,{field:'gmName', title: '审核人名称',templet:function (res) {
                        if(res.gmName==null){
                            return "无"
                        }else {
                           return res.gmName
                        }
                    }}
                ,{fixed: 'right', title:'操作', templet: '#switchStatus',width:180}
            ]]
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'views'){
                window.whEntryId = data.id;
                showWhEntryRD();

            } else if(obj.event === 'submitAudit'){
                var data1 = [];
                data1.push({"id":obj.data.id,"status":2});
                data = JSON.stringify(data1);
                layer.confirm('确认提交审核?', { btn: ['确认','取消'],btn1: function(){
                        $.ajax({
                            url: 'pk/warehouseEntry/updateWhEntryRStatus',
                            type: "POST",
                            data: data,
                            contentType:"application/json;charset=UTF-8",
                            success:function (data) {

                                layer.msg("提交审核成功！");
                                selectWhER();
                            }
                        });
                    },
                    btn2: function(){

                    }
                });
            }else if(obj.event === 'cancelAudit'){
                var data1 = [];
                data1.push({"id":obj.data.id,"status":1});
                data = JSON.stringify(data1);
                layer.confirm('确认取消审核?', { btn: ['确认','取消'],btn1: function(){
                        $.ajax({
                            url: 'pk/warehouseEntry/updateWhEntryRStatus',
                            type: "POST",
                            data: data,
                            contentType:"application/json;charset=UTF-8",
                            success:function (data) {
                                if(data.obj>=1){
                                    layer.msg("取消成功！");
                                    selectWhER();
                                }else {
                                    layer.msg(data.msg);
                                }
                            }
                        });
                    },
                    btn2: function(){

                    }
                });
            }else if(obj.event === 'delWhEntryR'){
                var whEntryId = obj.data.id;
                console.log(whEntryId);
                layer.confirm('确认删除吗？删除入库单会让对应货物状态重新变为待入库', { btn: ['确认','取消'],btn1: function(){
                        $.ajax({
                            url: 'pk/warehouseEntry/cancelWhEntryR',
                            type: "POST",
                            data: {
                                whEntryId : whEntryId
                            },
                            success:function (data) {
                                layer.msg("删除成功！");
                                selectWhER();
                            }
                        });
                    },
                    btn2: function(){

                    }
                });
            }else if(obj.event === 'onShelf'){
                // var data1 = layui.table.cache.test;
                // var goodsIdList = [];
                // for (var i=0;i<data1.length;i++) {
                //     goodsIdList.push(data1[i].goodsId);
                // }
                // window.goodsIdList = goodsIdList;

                window.whEntryId = obj.data.id;
                onShelf();
            }else if(obj.event === 'viewOnShelf'){
                window.whEntryId = obj.data.id;
                viewOnShelf();
            }
        });

        function onShelf() {
            layer.open({
                type: 2,
                area: ['800px','380px'],
                content: 'onShelives_insert2.html' //这里content是一个普通的String
            });
        }

        function viewOnShelf() {
            layer.open({
                type: 2,
                area: ['600px','380px'],
                content: 'onShelives_detail.html' //这里content是一个普通的String
            });
        }

        function showWhEntryRD() {
            layer.open({
                type: 2,
                area: ['600px','380px'],
                content: 'warehouseEntry_detail.html' //这里content是一个普通的String
            });
        }

    });

    function submitAudit(){
        var table = layui.table;
        var checkStatus = table.checkStatus('test');
        var data2 = checkStatus.data;//获取的所有
        if(data2.length<1){
            layer.alert("请选择至少一条再进行提交审核操作");
            // layer.alert(data[0].id);
        }else {
            var data1 = [];
            var flag = true;
            layer.confirm('确认提交审核?', { btn: ['确认','取消'],btn1: function(){
                    // var data = JSON.stringify(table.cache.test);
                    // console.log(data.length); 显示字符长度,不是数组长度

                    for (var i=0;i<data2.length;i++) {
                        if(data2[i].statusName==='新录入'||data2[i].status==='审核未通过'){
                            data1.push({"id":data2[i].id,"status":2});
                        }else {
                            flag = false;
                        }
                    }
                    var data = JSON.stringify(data1);
                    if(flag){
                        $.ajax({
                            url: 'pk/warehouseEntry/updateWhEntryRStatus',
                            type: "POST",
                            data: data,
                            contentType:"application/json;charset=UTF-8",
                            success:function (data) {
                                layer.msg("提交审核成功！");
                                selectWhER();
                                if(data.obj>=1){

                                }else {
                                    layer.msg(data.msg);
                                }

                            }
                        });
                    }else {
                        layer.alert("请只提交状态为新录入和审核不通过的入库单，不要提交其他状态入库单");
                    }
                },
                btn2: function(){

                }
            });
        }
    }
    function selectWhER() {
        layui.use('table', function () {
            var whEntryCode = $("#whEntryCode").val();
            var table = layui.table;
            table.reload('test', {
                page: {
                    curr: 1
                }, where: {
                    whEntryCode: whEntryCode
                }
            })
        })
    }
</script>
</body>
</html>